'use strict';

let nunjucks = require('nunjucks');

function createEnv(path, opts) {
    path = path || 'view';
    opts = opts || {};
    let confOpts = {
        autoescape: opts.autoescape || true,
        throwOnUndefind: opts.throwOnUndefind || false,
        IsTrimBlocks: opts.IsTrimBlocks || true,
        trimBlocks: opts.trimBlocks || true,
        watch: opts.watch || true,
        noCache: opts.noCache || true
    }
    let env = nunjucks.configure(path, confOpts);
    return env;
}

let njkMiddleWare = async (ctx, next) => {
    ctx.render = (view, model) => {
        let env = createEnv();
        ctx.body = env.render(view, model);
    }
    await next();
}

module.exports = njkMiddleWare;
